<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/23
 * Time: 16:52
 */

namespace app\api\controller;


use think\Db;

class Map extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    private $order_number;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $this->order_number = date('YmdHis') . rand(1000, 9999);
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    public function getCitys()
    {
//        header("Content-type: text/html; charset=utf-8");
        $where = [
            'level' => 2
        ];
        if(isset($_POST['key']) && $_POST['key']){
            $where['district'] = ['like','%' . $_POST['key'] . '%'];
        }
        $data = Db::table('up_area')
            ->where($where)
            ->field('district_id id,district name,letter')
            ->order('letter')
            ->select();
        $start = '';
        $res = array();
        foreach ($data as $k=>$v){
            if($v['letter'] != $start){
                $start = $v['letter'];
            }
            $res[$start][] = $v;
        }
        //--------------------
        return json(['status'=>200,'msg'=>$res]);
//    dump($data);
//    dump($res);

    }
    /***
     * @return \think\response\Json
     * 更新城市json数据
     */
    public function makejsonareas(){
        set_time_limit(0);
        $pro = Db::table('up_area')
            ->where('level',1)
//            ->where('district_id',1001)
            ->field('district_id id,district name')
            ->select();
        $shi = Db::table('up_area')
            ->where('level',2)
            ->field('district_id id,district name,pid parentId')
            ->select();
        $qu = Db::table('up_area')
            ->where('level',3)
            ->field('district_id id,district name,pid parentId')
            ->select();
        foreach ($shi as $k=>$v){
            foreach ($qu as $kk=>$vv){
                if($vv['parentId'] == $v['id']){
                    $shi[$k]['sub'][] = $vv;
                }
            }
        }
        foreach($pro as $k=>$v){
            $pro[$k]['parentId'] = 0;
            foreach ($shi as $kk=>$vv){
                if($vv['parentId'] == $v['id']){
                    $pro[$k]['sub'][] = $vv;
                }
            }
        }
        $root = $_SERVER['DOCUMENT_ROOT'];
        $myfile = fopen("$root/static/area.json", "w") or die("Unable to open file!");
        $txt = json_encode($pro);
        fwrite($myfile, $txt);
        fclose($myfile);
        return json(['code' => 200, 'msg' => '更新成功']);
    }
    public function getLetter($num){
        $limit = array( //gb2312 拼音排序
            array(45217,45252), //A
            array(45253,45760), //B
            array(45761,46317), //C
            array(46318,46825), //D
            array(46826,47009), //E
            array(47010,47296), //F
            array(47297,47613), //G
            array(47614,48118), //H
            array(0,0),     //I
            array(48119,49061), //J
            array(49062,49323), //K
            array(49324,49895), //L
            array(49896,50370), //M
            array(50371,50613), //N
            array(50614,50621), //O
            array(50622,50905), //P
            array(50906,51386), //Q
            array(51387,51445), //R
            array(51446,52217), //S
            array(52218,52697), //T
            array(0,0),     //U
            array(0,0),     //V
            array(52698,52979), //W
            array(52980,53688), //X
            array(53689,54480), //Y
            array(54481,55289), //Z
        );
        $char_index=65;
        foreach($limit as $k=>$v){
            if($num>=$v[0] && $num<=$v[1]){
                $char_index+=$k;
                return $char_index;
            }
        }
        return -1;
    }
}